import 'package:flutter/cupertino.dart';
import 'package:mvvm_provider/page/novel/data/model/content_model_entity.dart';
import 'package:mvvm_provider/page/novel/data/model/tabs_model_entity.dart';
import 'package:mvvm_provider/page/novel/data/repository/novel_repository.dart';
import 'package:mvvm_provider/page/novel/states/novel_detail_state.dart';
import '../../../base/base_view_model.dart';
import '../../../model/response_model.dart';

class NovelDetailViewModel extends BaseViewModel<NovelDetailState> {
  NovelDetailViewModel() : super(NovelDetailState());

  Future<void> getTabsData() async {
    ResponseModel? responseModel = await NovelRepository.getNovelTabsData<TabsModelData>();
    TabsModelData tabsModelData = responseModel.data;
    state.tabsList = tabsModelData.list;
    notifyListeners();
  }

  Future<void> getContentData({int id = 7}) async {
    ResponseModel? responseModel =
        await NovelRepository.getNovelContentData<ContentModelData>(id: id);
    ContentModelData contentModelData = responseModel.data;
    state.contentList = contentModelData.list;
    scrollController.animateTo(0,
        duration: const Duration(milliseconds: 400), curve: Curves.easeIn);
    refreshController.refreshCompleted();
    refreshController.loadComplete();
    notifyListeners();
  }

  /// 改变左边点击tab
  void changeIndex(int index) {
    if (index == state.selectIndex) return;
    if (index >= state.tabsList.length) {
      refreshController.refreshCompleted();
      refreshController.loadComplete();
      refreshController.loadNoData();
      return;
    }
    if (index < 0) {
      refreshController.refreshCompleted();
      refreshController.loadComplete();
      return;
    }
    state.selectIndex = index;
    getContentData(id: state.tabsList[index].id);
  }
}
